// MyGraphClockView.cpp : implementation file
//

#include "stdafx.h"
#include "waveeditor.h"
#include "waveeditorDoc.h"
#include "MyGraphClockView.h"
#include "MyGraphView.h"
#include "MyGraphFrame.h"
#include ".\mygraphclockview.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// MyGraphClockView

IMPLEMENT_DYNCREATE(GraphClockView, CScrollView)

GraphClockView::GraphClockView()
{
}

GraphClockView::~GraphClockView()
{
}


BEGIN_MESSAGE_MAP(GraphClockView, CScrollView)
	//{{AFX_MSG_MAP(MyGraphClockView)
	ON_WM_MOUSEMOVE()
	ON_WM_KILLFOCUS()
	//}}AFX_MSG_MAP
	ON_WM_HSCROLL()
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// MyGraphClockView drawing

void GraphClockView::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();


	// TODO: calculate the total size of this view

	CSize sizeTotal;
	// TODO: calculate the total size of this view
	sizeTotal.cx	= 0;//pDoc->_script[SelectedScenario]->mygraphdata.GetNumberOfSignals()*pDoc->_script[SelectedScenario]->mygraphdata.GetDeltax();
	sizeTotal.cy = 50;
	SetScrollSizes(MM_TEXT, sizeTotal);
	SelectedScenario = GetDocument()->selectedscenario;
}

void GraphClockView::OnDraw(CDC* pDC)
{
	CRect winrect;
	GetClientRect(&winrect);
	ZWaveeditorDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	scenario *CurrentScenario = pDoc->_script[SelectedScenario];
	LOGFONT lf;
	memset(&lf, 0, sizeof(LOGFONT));          
	lf.lfHeight = 12; 
//	lf.lfPitchAndFamily  = FIXED_PITCH;
	strcpy(lf.lfFaceName, "Courier");           

	CFont font;
	font.CreateFontIndirect(&lf);  // create the font
	CFont *oldfont	=	pDC->SelectObject(&font);

	
//-----------------------------------------------------------------
	CPoint pnt[8];
	CPen pen(PS_SOLID,1, RGB(128,128,128));
	CPen* pOldPen			=	pDC->SelectObject(&pen);
	pnt[0].y				=	15;
	pnt[1].y				=	30;
	double delta			=	winrect.right / QUANTITY_HYPHENS;
	CString		oldstrpos	=	"";
	int updown				=	0;

	//long StartDrawingCoordinateX = si.nPos - GetParentGraphFrame()->GetGraphView()->GetIndentOfGraphFromWindow();
	long StartDrawingCoordinateX = GetParentGraphFrame()->GetGraphView()->GetIndentOfGraphFromWindow();
	pDC->SetViewportOrg( -StartDrawingCoordinateX,0 );
	if((CurrentScenario->mygraphdata.graphdelta/2) < CurrentScenario->mygraphdata.globaldelta)
	{
		for( int i = 0; i < 15 ; i++ )
		{
			pnt[0].x	=	pnt[1].x	=	i*delta;
			CString strpos =	CurrentScenario->mygraphdata.GetTrueCoordinates(pnt[0]);
			oldstrpos	=	strpos;

			//Repainting will begin from current scroll position
			//pnt[0].x=pnt[1].x+=StartDrawingCoordinateX;
			BOOL Result = pDC->Polyline(pnt,2);
			pDC->TextOut(pnt[0].x,5+30*(updown%2),oldstrpos);	
			updown++;
		}
	}
	else
	{
		__int64 grdelta		=	0;
		int step		=	1;
		while(grdelta < CurrentScenario->mygraphdata.globaldelta)
		{
			pnt[0].x	=	pnt[1].x	=	CurrentScenario->mygraphdata.GetWindowCoordinate(grdelta+CurrentScenario->mygraphdata.globaloffset);
			CString strpos		=	CurrentScenario->mygraphdata.GetTrueCoordinates(pnt[0]);
			oldstrpos	=	strpos;

			//Repainting will begin from current scroll position
			//pnt[0].x=pnt[1].x+=StartDrawingCoordinateX;
			pDC->Polyline(pnt,2);
			pDC->TextOut(pnt[0].x,5+30*(updown%2),oldstrpos);	
			updown++;
			if(CurrentScenario->mygraphdata.globaldelta >14)
			{
				grdelta	= ((step*CurrentScenario->mygraphdata.globaldelta) / 14);
			}
			else
			{
				grdelta	= step;
			}
			step ++;
		}
	}
	pDC->SelectObject(pOldPen);
	pDC->SelectObject(oldfont);
	pDoc->linesforselection[1].right = -1;
}

/////////////////////////////////////////////////////////////////////////////
// MyGraphClockView diagnostics

#ifdef _DEBUG
void GraphClockView::AssertValid() const
{
	CScrollView::AssertValid();
}

void GraphClockView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}
ZWaveeditorDoc* GraphClockView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(ZWaveeditorDoc)));
	return (ZWaveeditorDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// MyGraphClockView message handlers

void GraphClockView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	// TODO: Add your specialized code here and/or call the base class
	UpdateView();
}

void GraphClockView::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	
	CScrollView::OnMouseMove(nFlags, point);
	ZWaveeditorDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	//pDoc->OnGraphMouseMove(point);
}




void GraphClockView::ChangeWindowTitle()
{
	CFrameWnd* pFrame	=	GetParentFrame();
	ZWaveeditorDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	CString	name	=	"Graph View (";
	name			+=	pDoc->GetTitle()+")" + " - "+pDoc->GroupName;
	pFrame->SetWindowText(name);
}

void GraphClockView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView) 
{
	// TODO: Add your specialized code here and/or call the base class
	CScrollView::OnActivateView(bActivate, pActivateView, pDeactiveView);
	ChangeWindowTitle();
}

void GraphClockView::OnKillFocus(CWnd* pNewWnd) 
{
	CScrollView::OnKillFocus(pNewWnd);
	ChangeWindowTitle();
	// TODO: Add your message handler code here
	
}

int GraphClockView::UpdateView(void)
{
	Invalidate();
	ChangeWindowTitle();
	return 0;
}

ChildGraphFrame * GraphClockView::GetParentGraphFrame()
{
	return (ChildGraphFrame *)GetParentFrame();
}

void GraphClockView::GetWholeGraphClockChart( CImage *ImageForSave, graphdata *ImageGraphData )
{
	CDC *GraphViewDC = CDC::FromHandle( ImageForSave->GetDC() );
	DrawWholeGraphClockChart( GraphViewDC, ImageGraphData );
	ImageForSave->ReleaseDC();
}

void GraphClockView::DrawWholeGraphClockChart(CDC* pDC, graphdata *ImageGraphData)
{
	ZWaveeditorDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	LOGFONT lf;
	memset(&lf, 0, sizeof(LOGFONT));          
	lf.lfHeight = 12; 
	strcpy(lf.lfFaceName, "Courier");           
	__int64 HorizontalSize = ImageGraphData->GetSizeInPixels( ImageGraphData->GetPageLength() );

	CFont font;
	font.CreateFontIndirect(&lf);  // create the font
	CFont *oldfont	=	pDC->SelectObject(&font);

	
//-----------------------------------------------------------------
	CPoint pnt[8];
	CPen pen(PS_SOLID,1, RGB(128,128,128));
	CPen* pOldPen			=	pDC->SelectObject(&pen);
	pnt[0].y				=	15;
	pnt[1].y				=	30;
	double delta			=	(pDoc->graphwindowMaxX) / 14;
	CString		oldstrpos	=	"";
	int updown				=	0;
	int NumberOfMarks = HorizontalSize / delta + 1;
	int step		=	0;
	GetParentGraphFrame()->GetGraphView()->DrawRectangleInColor( pDC, CRect( 0, 0, HorizontalSize, 50 ), WHITE, WHITE );
	pDC->SetBkColor		( WHITE );
	if( (ImageGraphData->graphdelta/2) < ImageGraphData->globaldelta)
	{
		delta = HorizontalSize / NumberOfMarks;
		int XPos = 0;
		while(XPos < HorizontalSize )
		{
			pnt[0].x	=	pnt[1].x	= XPos =	int( step * delta );
			CString strpos		=	ImageGraphData->GetTrueCoordinates(pnt[0]);
			oldstrpos	=	strpos;
				//Repainting will begin from current scroll position
			pDC->Polyline(pnt,2);
			pDC->TextOut(pnt[0].x,5+30*(updown%2),oldstrpos);	
			updown++;
			step ++;
		}
	}
	else
	{
		int grdelta		=	0;
		step		=	1;
		while(grdelta < ImageGraphData->globaldelta)
		{
			pnt[0].x	=	pnt[1].x	=	ImageGraphData->GetWindowCoordinate( grdelta + ImageGraphData->globaloffset );
			CString strpos		=	ImageGraphData->GetTrueCoordinates( pnt[0] );
			oldstrpos	=	strpos;

			//Repainting will begin from current scroll position
			pDC->Polyline(pnt,2);
			pDC->TextOut(pnt[0].x,5+30*(updown%2),oldstrpos);	
			updown++;
			if( ImageGraphData->globaldelta > NumberOfMarks )
			{
				grdelta	= ((step*ImageGraphData->globaldelta) / NumberOfMarks);
			}
			else
			{
				grdelta	= step;
			}
			step ++;
		}
	}
	pDC->SelectObject(pOldPen);
	pDC->SelectObject(oldfont);
}

void GraphClockView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
}
